//
//  CommentScreenView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/7/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "CommentScreenView.h"
#import "QuartzCore/QuartzCore.h"
#import "ZooFoodBean.h"
#import "RemoveFoodAlertView.h"
#import "PickImageSourceAlertView.h"
#import "ELCAlbumPickerController.h"
#import "ELCImagePickerController.h"
#import "ZooUtil.h"
#import "UIImage+Crop.h"

@implementation CommentScreenView
@synthesize parent, arFoodChecked = _arFoodChecked, arFoodNoneCheck = _arFoodNoneCheck,arPicture=_arPicture;
@synthesize vImgFood = _vImgFood;
@synthesize btnGallery = _btnGallery;

#define TEXTVIEW_COMMENT_WIDTH 243
#define TEXTVIEW_COMMENT_HEIGHT 220
#define PADDING_SIZE 6
#define IMAGE_FOOD_SIZE 48
#define VIEW_CONTENTFOOD_SIZE 107
#define CORNER_RADIUS 7.0

typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        parent = commentViewController;
        self.delegate = self;
        self.dataSource = self;
        
        stateTwitter = FALSE;
        stateFacebook = FALSE;
        stateTumblr = FALSE;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame noneCheckArray:(NSMutableArray *)noneCheckArray checkedArray:(NSMutableArray *)checkedArray {
    self = [super initWithFrame:frame];
    if (self) {
        _arPicture = [[NSMutableArray alloc]init];
        _arFoodNoneCheck = noneCheckArray;
        _arFoodChecked = checkedArray;
        stateTwitter = FALSE;
        stateFacebook = FALSE;
        stateTumblr = FALSE;
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return 230;
        case 1:
            return 150;
        case 2:
            return 30;
        default:
            return 1;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier1 = @"cellIdentifier1";
    
    UITableViewCell *_cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    if (_cell == nil) {
        _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];
        [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    switch (indexPath.row) {
        case 0:
            [self initViewComment:_cell];
            break;
        case 1:
            [self initViewAddress:_cell];
            break;
        case 2:
            [self initViewFooter:_cell];
            break;
        default:
            break;
    }
    
    return _cell;
}

-(void)initViewComment:(UITableViewCell *)cell {
    _txtComment = [[UITextView alloc]initWithFrame:CGRectMake(PADDING_SIZE+1, PADDING_SIZE+1, TEXTVIEW_COMMENT_WIDTH-5, TEXTVIEW_COMMENT_HEIGHT-20)];

    UIImageView *_vImgBackground = [[UIImageView alloc]initWithFrame:CGRectMake(PADDING_SIZE, 5, TEXTVIEW_COMMENT_WIDTH, TEXTVIEW_COMMENT_HEIGHT)];
    
    _imgBackground = [[UIImage imageNamed:IMG_TEXTVIEW_COMMENT]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

    [_vImgBackground setImage:_imgBackground];
    
    _txtComment.delegate = self;
    [_txtComment setFont:[UIFont systemFontOfSize:14]];
    _txtComment.selectedRange = NSMakeRange(0, 140);
    _txtComment.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    _lbPlaceHolder = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, _txtComment.frame.size.width - 20, 34)];
    [_lbPlaceHolder setText:STRING_COMMENT];
    [_lbPlaceHolder setBackgroundColor:[UIColor clearColor]];
    [_lbPlaceHolder setFont:[UIFont systemFontOfSize:14]];
    [_lbPlaceHolder setTextColor:[UIColor lightGrayColor]];
    
    [_txtComment addSubview:_lbPlaceHolder];
    
    _btnGallery = [[UIButton alloc]initWithFrame:CGRectMake(TEXTVIEW_COMMENT_WIDTH + PADDING_SIZE*2, 5, 58, 176)];
    [_btnGallery addTarget:self action:@selector(btnGalleryClicked) forControlEvents:UIControlEventTouchUpInside];
    [_btnGallery setBackgroundColor:[UIColor grayColor]];
    
    for (int i=0; i<3; i++) {
        _vImgFood = [[UIImageView alloc]initWithFrame:CGRectMake(PADDING_SIZE-1, PADDING_SIZE*(i+1) + IMAGE_FOOD_SIZE*i, IMAGE_FOOD_SIZE, IMAGE_FOOD_SIZE)];            
        [_vImgFood setImage:[UIImage imageNamed:IMG_BUTTON_GALLERY]];
        [_btnGallery addSubview:_vImgFood];
        [_vImgFood release];
    }
    
    _vIconMore = [[UIImageView alloc]initWithFrame:CGRectMake((_btnGallery.bounds.size.width-25) /2, PADDING_SIZE*4+IMAGE_FOOD_SIZE*3, 25, 5)];
    [_vIconMore setImage:[UIImage imageNamed:IMG_ICON_MORE]];
    [_btnGallery addSubview:_vIconMore];
    [_vIconMore release];
    
    _btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(TEXTVIEW_COMMENT_WIDTH + PADDING_SIZE*2, 187, 58, 38)];
    [_btnCamera addTarget:self action:@selector(btnCameraClicked) forControlEvents:UIControlEventTouchUpInside];
    [_btnCamera setBackgroundImage:[UIImage imageNamed:IMG_CAMERA] forState:UIControlStateNormal];
    _vImgCamera = [[UIImageView alloc]initWithFrame:CGRectMake((_btnCamera.bounds.size.width -29) /2, 8, 29, 22)];
//    [_vImgCamera setImage:[UIImage imageNamed:IMG_CAMERA]];
//    [_btnCamera addSubview:_vImgCamera];
    _imagePicker = [[UIImagePickerController alloc]init];
    _imagePicker.allowsEditing = YES;
    _imagePicker.delegate = parent;
    
    [cell.contentView addSubview:_vImgBackground];
    [cell.contentView addSubview:_txtComment];
    [cell.contentView addSubview:_btnGallery];
    [cell.contentView addSubview:_btnCamera];
    
    [_vImgBackground release];
    
}

-(void)initViewAddress:(UITableViewCell *)cell {
    _vFood = [[UIView alloc]initWithFrame:CGRectMake(PADDING_SIZE, PADDING_SIZE, 107, 145)];
    [_vFood setBackgroundColor:[UIColor clearColor]];
    _btnPickFood = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 107, 26)];
    [_btnPickFood setBackgroundImage:[UIImage imageNamed:IMG_PICKFOOD] forState:UIControlStateNormal];
    [_btnPickFood setTitle:STRING_BTN_FOOD forState:UIControlStateNormal];
    [_btnPickFood addTarget:self action:@selector(btnPickFoodClicked) forControlEvents:UIControlEventTouchUpInside];

    [_vFood addSubview:_btnPickFood];

    
    [self initScrollViewFood];
    
    _btnAddress = [[UIButton alloc]initWithFrame:CGRectMake(107 + PADDING_SIZE*4-2, PADDING_SIZE, 186, 145)];
    _btnPickAddress = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 186, 26)];
    [_btnPickAddress setTitle:@"83B Ly Thuong Kiet" forState:UIControlStateNormal];
    [_btnPickAddress setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_PICKADDRESS]]];
    [_btnPickAddress setBackgroundImage:[UIImage imageNamed:IMG_PICKADDRESS] forState:UIControlStateNormal];
    UIImageView *_vImgBackground = [[UIImageView alloc]initWithFrame:CGRectMake(0, 26+PADDING_SIZE, 186, 106)];
    
    _imgBackground = [[UIImage imageNamed:IMG_TEXTVIEW_COMMENT]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
//    _imgBackground = [UIImage imageNamed:IMG_TEXTVIEW_COMMENT];
    
    [_vImgBackground setImage:_imgBackground];
    
    _mvAddress = [[MKMapView alloc]initWithFrame:CGRectMake(2, 2, 182, 102)];
    [_mvAddress setMapType:MKMapTypeStandard];
//    _mvAddress.showsUserLocation = YES;
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
//    region.center = location;
    region.center.latitude = 21.0418241 ;
    region.center.longitude = 105.8401011;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [_mvAddress setRegion:region animated:YES];
    
    [_vImgBackground addSubview:_mvAddress];
    
    [_btnAddress addSubview:_btnPickAddress];
    [_btnAddress addSubview:_vImgBackground];
    
    [cell.contentView addSubview:_vFood];
    [cell.contentView addSubview:_btnAddress];
    
    [_vImgBackground release];
}

-(void)initScrollViewFood {
    UIButton *a = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [a setTitle:@"abc" forState:UIControlStateNormal];
    
    int numOfFood = [_arFoodChecked count];
    int numOfPage = numOfFood/4;
    int mod = numOfFood%4;

    _svFood = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 26+PADDING_SIZE, 107, 105)];
    [_svFood setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    
    if (mod!=0 || numOfPage==0 ) {
        numOfPage += 1;
    }
    
    _svFood.contentSize = CGSizeMake(VIEW_CONTENTFOOD_SIZE*numOfPage, 105);
    _svFood.pagingEnabled = YES;
    _svFood.showsHorizontalScrollIndicator = NO;

    
    [_svFood addSubview:a];
    
    for (int i=0; i<numOfPage; i++) {
        UIView *viewContentNoneFood = [[UIView alloc]initWithFrame:CGRectMake(VIEW_CONTENTFOOD_SIZE*i, 0, VIEW_CONTENTFOOD_SIZE, 105)];
        UIButton *btnNoneFood;
        for (int j=0; j < 4; j++) {

            btnNoneFood = [[UIButton alloc]init];
            [btnNoneFood addTarget:self action:@selector(btnNoneFoodClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [btnNoneFood setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_BG_DISH]]];
            
            switch (j) {
                    
                case 0:
                    btnNoneFood.frame = CGRectMake(2, 0, 49, 49);
                    [viewContentNoneFood addSubview:btnNoneFood];
                    break;
                case 1:
                    btnNoneFood.frame = CGRectMake(56, 0, 49, 49);
                    [viewContentNoneFood addSubview:btnNoneFood];
                    break;                
                case 2:
                    btnNoneFood.frame = CGRectMake(2, 54, 49, 49);
                    [viewContentNoneFood addSubview:btnNoneFood];
                    break;
                case 3:
                    btnNoneFood.frame = CGRectMake(56, 54, 49, 49);
                    [viewContentNoneFood addSubview:btnNoneFood];
                    break;
                default:
                    break;
            }
            btnNoneFood.tag = j;
            [btnNoneFood release];
        }
        [_svFood addSubview:viewContentNoneFood];
        [viewContentNoneFood release];
    }
    
    for (int i=0; i<numOfPage; i++) {
        UIView *viewContentFood = [[UIView alloc]initWithFrame:CGRectMake(VIEW_CONTENTFOOD_SIZE*i, 0, VIEW_CONTENTFOOD_SIZE, 105)];
        UIButton *btnFoodItem; 
        int intTag;
        
        if (i < numOfPage-1) {

            for (int j=0; j < 4; j++) {
                btnFoodItem = [[UIButton alloc]init];
                [btnFoodItem addTarget:self action:@selector(btnDetailFoodClicked:) forControlEvents:UIControlEventTouchUpInside];
                btnFoodItem.layer.cornerRadius = CORNER_RADIUS;
                btnFoodItem.layer.masksToBounds = YES;
                [btnFoodItem setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickfood_img.png"]]];
                
                switch (j) {        
                    case 0:
                        btnFoodItem.frame = CGRectMake(2, 0, 49, 49);
                        [viewContentFood addSubview:btnFoodItem];
                        break;
                    case 1:
                        btnFoodItem.frame = CGRectMake(56, 0, 49, 49);
                        [viewContentFood addSubview:btnFoodItem];
                        break;                
                    case 2:
                        btnFoodItem.frame = CGRectMake(2, 54, 49, 49);
                        [viewContentFood addSubview:btnFoodItem];
                        break;
                    case 3:
                        btnFoodItem.frame = CGRectMake(56, 54, 49, 49);
                        [viewContentFood addSubview:btnFoodItem];
                        break;
                    default:
                        break;
                }
                
                intTag = i*4+j;
                btnFoodItem.tag = intTag;
                
                [btnFoodItem release];
            }
            
        } else if (i == (numOfPage -1)) {
            if (mod == 0 && numOfFood!=0) {
                
                for (int j=0; j < 4; j++) {
                    btnFoodItem = [[UIButton alloc]init];
                    [btnFoodItem addTarget:self action:@selector(btnDetailFoodClicked:) forControlEvents:UIControlEventTouchUpInside];
                    btnFoodItem.layer.cornerRadius = CORNER_RADIUS;
                    btnFoodItem.layer.masksToBounds = YES;
                    [btnFoodItem setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickfood_img.png"]]];
                    
                    switch (j) {      
                        case 0:
                            btnFoodItem.frame = CGRectMake(2, 0, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        case 1:
                            btnFoodItem.frame = CGRectMake(56, 0, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;                
                        case 2:
                            btnFoodItem.frame = CGRectMake(2, 54, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        case 3:
                            btnFoodItem.frame = CGRectMake(56, 54, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        default:
                            break;
                    }
                    
                    intTag = i*4+j;
                    btnFoodItem.tag = intTag;
                    [btnFoodItem release];
                }
            } else {

                for (int j=0; j < mod; j++) {
                    btnFoodItem = [[UIButton alloc]init];

                    [btnFoodItem addTarget:self action:@selector(btnDetailFoodClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [btnFoodItem setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickfood_img.png"]]];
                    btnFoodItem.layer.cornerRadius = CORNER_RADIUS;
                    btnFoodItem.layer.masksToBounds = YES;

                    switch (j) {            
                        case 0:
                            btnFoodItem.frame = CGRectMake(2, 0, 49, 49);

                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        case 1:
                            btnFoodItem.frame = CGRectMake(56, 0, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;                
                        case 2:
                            btnFoodItem.frame = CGRectMake(2, 54, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        case 3:
                            btnFoodItem.frame = CGRectMake(56, 54, 49, 49);
                            [viewContentFood addSubview:btnFoodItem];
                            break;
                        default:
                            break;
                    }
                    
                    intTag = i*4+j;
                    btnFoodItem.tag = intTag;
                    
                    [btnFoodItem release];
                }
            }
        }
        [_svFood addSubview:viewContentFood];
        [viewContentFood release];
        
    }
    
    [_vFood addSubview:_svFood];
    [_svFood release];
}

-(void)initViewFooter:(UITableViewCell *)cell {
    UIView *_vLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 2)];
    [_vLine setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_LINE]]];

    _btnTwitter = [[UIButton alloc]initWithFrame:CGRectMake(222, 3, 21, 21)];
    [_btnTwitter setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TWITTER_NORMAL]]];
    [_btnTwitter addTarget:self action:@selector(btnTwitterClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _btnFacebook = [[UIButton alloc]initWithFrame:CGRectMake(258, 3, 21, 21)];
    [_btnFacebook setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_FACEBOOK_NORMAL]]];
    [_btnFacebook addTarget:self action:@selector(btnFacebookClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _btnTumblr = [[UIButton alloc]initWithFrame:CGRectMake(294, 3, 21, 21)];
    [_btnTumblr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TUMBLR_NORMAL]]];
    [_btnTumblr addTarget:self action:@selector(btnTumblrClicked) forControlEvents:UIControlEventTouchUpInside];    
    
    [cell.contentView addSubview:_vLine];
    [cell.contentView addSubview:_btnTwitter];
    [cell.contentView addSubview:_btnFacebook];
    [cell.contentView addSubview:_btnTumblr];    
}

#pragma mark - Control txtComment
- (void)textViewDidChange:(UITextView *)textView {
    if(![_txtComment hasText]) {
        [_txtComment addSubview:_lbPlaceHolder];
        [UIView animateWithDuration:0.15 animations:^{
            _lbPlaceHolder.alpha = 1.0;
        }];
    } else if ([[_txtComment subviews] containsObject:_lbPlaceHolder]) {
        [UIView animateWithDuration:0.15 animations:^{
            _lbPlaceHolder.alpha = 0.0;
        } completion:^(BOOL finished) {
            [_lbPlaceHolder removeFromSuperview];
        }];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (![_txtComment hasText]) {
        [_txtComment addSubview:_lbPlaceHolder];
        [UIView animateWithDuration:0.15 animations:^{
            _lbPlaceHolder.alpha = 1.0;
        }];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    NSUInteger oldLength = [_txtComment.text length];
    NSUInteger replacementLength = [text length];
    NSUInteger rangeLength = range.length;
    
    NSUInteger newLength = oldLength - rangeLength + replacementLength;
    
    if([text isEqualToString:@"\n"]) {
        [_txtComment resignFirstResponder];
        return NO;
    }
    
    return newLength <= MAX_COMMENT_LENGTH;
}

#pragma mark - alert view delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        if (buttonIndex == 1) {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            UIButton *btnPick = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btnPick.frame = CGRectMake(280, 5, 30, 20);
            [btnPick setTitle:@"Open" forState:UIControlStateNormal];
            UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                            style:UIBarButtonItemStyleDone target:nil action:nil];
            _imagePicker.navigationItem.rightBarButtonItem = rightButton;
            _imagePicker.navigationBarHidden = YES;
            _imagePicker.navigationBar.tintColor = [UIColor orangeColor];
            _imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            
            [parent presentModalViewController:_imagePicker animated:YES];
        } else if (buttonIndex == 2) {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //        _imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [parent presentModalViewController:_imagePicker animated:YES];
        } 
    } else if (alertView.tag == 2) {
        if (buttonIndex == 1) {
            [_arFoodNoneCheck addObject:[_arFoodChecked objectAtIndex:_numberPick]];
            [_arFoodChecked removeObjectAtIndex:_numberPick];
            [self initScrollViewFood];
        }
    }
    
}

#pragma mark - Image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
    _vImgFood.image = [image imageByScalingAndCroppingForSize:CGSizeMake(IMAGE_FOOD_SIZE, IMAGE_FOOD_SIZE)];
    [picker dismissModalViewControllerAnimated:YES];
}



//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//    NSLog(@"Media info: %@", info);
//    
//    if (_imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//            //obtaining saving path
//        
////        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
////        NSLog(@"%@",paths);
////        NSString *documentsDirectory = [paths objectAtIndex:0];
////        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"../latest_photo.png"];
////        NSLog(@"%@",imagePath);
//        
////        NSString *imageURL = nil;
//        
//        
//        
//            //extracting image from the picker and saving it
//        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   
//        if ([mediaType isEqualToString:@"public.image"]){
//            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
//            
////            NSData *webData = UIImagePNGRepresentation(editedImage);
////            [webData writeToFile:imagePath atomically:YES];
//            
//            UIImageWriteToSavedPhotosAlbum(editedImage, self, nil, nil);
//            [_arPicture addObject:[editedImage copy]];
//            _vImgFood.image = editedImage;
//            [parent dismissModalViewControllerAnimated:YES];
//            ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error){
//                if (!error) {  
//                        //                imageURL = [NSString stringWithFormat:@"%@",assetURL];
//                    NSLog(@"%@",assetURL);
//                }  else {
//                    NSLog(@"%@",assetURL);
//                }
//            };
//        }
//        
//    } else if (_imagePicker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//        
//        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   
//        if ([mediaType isEqualToString:@"public.image"]){
//            
//            
//            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
//            _vImgFood.image = editedImage;
//            [_arPicture addObject:[editedImage copy]];
//            [parent dismissModalViewControllerAnimated:YES];
//        }
//    }
//    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
//    NSLog(@"%@",referenceURL);
//    
//    NSLog(@"%d",[_arPicture count]);
//    
//}

#pragma mark - PickImageSourceDelegate

- (void)pickCameraSource {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [_imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [_imagePicker setAllowsEditing:YES];
        [_imagePicker setDelegate:parent];
        [parent presentModalViewController:_imagePicker animated:YES];
    } else {
        [ZooUtil showSimpleAlert:@"Camera Unavaible"];
    }
}

- (void)pickGallerySource {
        //    [_imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        //    [_imagePicker setAllowsEditing:YES];
        //
        //    [_parentView presentModalViewController:_imagePicker animated:YES];
    
    ELCAlbumPickerController *_albumPicker = [[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle]];
    ELCImagePickerController *_elcImagePicker = [[ELCImagePickerController alloc] initWithRootViewController:_albumPicker];
    [_albumPicker setParent:_elcImagePicker];
    [_elcImagePicker setDelegate:parent];
    
    [parent presentModalViewController:_elcImagePicker animated:YES];
    
    [_albumPicker release];
    [_elcImagePicker release];
}

#pragma mark - Control event button clicked

- (void)btnGalleryClicked {
    [parent pushViewPictureScreen];
}

- (void)btnCameraClicked {

    PickImageSourceAlertView *_pickImageSourceAlertView = [[PickImageSourceAlertView alloc] initWithTitle:nil message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil];
    
    _pickImageSourceAlertView.pickSourceDelegate = self;
    [_pickImageSourceAlertView show];
    [_pickImageSourceAlertView release];
}

- (void)btnPickFoodClicked {
//    CommentViewController *commentView = parent;
    if ([_arFoodChecked count]==0) {
        _arFoodNoneCheck = [[NSMutableArray alloc]init];
        _arFoodChecked = [[NSMutableArray alloc]init];
        for (int i=0; i<10; i++) {
            ZooFoodBean *foodBean = [[ZooFoodBean alloc]init];
            foodBean.foodId = [NSString stringWithFormat:@"%d",i];
            foodBean.foodName = [NSString stringWithFormat:@"Pho cuon %d",i];
            foodBean.foodCost = [NSString stringWithFormat:@"5%d",i];
            [_arFoodNoneCheck addObject:foodBean];
            [foodBean release];
        }

        [parent pushPickFoodScreen:_arFoodChecked arrayNoneCheck:_arFoodNoneCheck];
    } else {
        [parent pushPickFoodScreen:_arFoodChecked arrayNoneCheck:_arFoodNoneCheck];
    }

}

- (void)btnDetailFoodClicked:(UIButton *)button {
    ZooFoodBean *foodBean = [_arFoodChecked objectAtIndex:button.tag];
    _numberPick = button.tag;
    NSLog(@"Food name: %@",foodBean.foodName);
    RemoveFoodAlertView *removeAlertView = [[RemoveFoodAlertView alloc]initWithTitle:@"                                    " message:@".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .                                                                                                                                                                                                                                                                          .                                                                                                                                                                                                                                                                          .                                                                                                                                                                                                                                                                          .                                                                                                                                                                                                                                                                          .                                                                                                                                                                                                                                                                          .                                                                                                                                                                                                                                                                          ." delegate:self cancelButtonTitle:STRING_CANCEL otherButtonTitles:@"Xóa món", nil];
    removeAlertView.foodBean = foodBean;
    removeAlertView.tag = 2;
    [removeAlertView show];
}

- (void)btnNoneFoodClicked:(UIButton *)button {
    NSLog(@"none food clicked:%d",button.tag);
}

-(void)btnTwitterClicked {
    if (!stateTwitter) {
        [_btnTwitter setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TWITTER_SELECTED]]];
        stateTwitter = TRUE;
    } else {
        [_btnTwitter setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TWITTER_NORMAL]]];
        stateTwitter = FALSE;        
    }
}

-(void)btnFacebookClicked {
    if (!stateFacebook) {
        [_btnFacebook setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_FACEBOOK_SELECTED]]];  
        stateFacebook = TRUE;
    } else {
        [_btnFacebook setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_FACEBOOK_NORMAL]]];    
        stateFacebook = FALSE;
    }
}

-(void)btnTumblrClicked {
    if (!stateTumblr) {
        [_btnTumblr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TUMBLR_SELECTED]]];
        stateTumblr = TRUE;
    } else {
        [_btnTumblr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ICON_TUMBLR_NORMAL]]];
        stateTumblr = FALSE;
    }
}

@end
